package com.yulinlin.admin.modules.sys.service.base;

import com.yulinlin.admin.modules.sys.domain.SysElementEntity;
import com.yulinlin.admin.modules.sys.domain.SysElementRelEntity;
import com.yulinlin.admin.modules.sys.domain.join.SysElementRelJoin;
import com.yulinlin.admin.modules.sys.service.ISysElementRelService;
import com.yulinlin.admin.modules.sys.dao.SysElementRelDao;

import com.yulinlin.data.core.model.BaseModelDeleteWrapper;
import com.yulinlin.data.core.model.BaseModelSelectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.yulinlin.common.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

@Service

public class SysElementRelServiceImpl extends ServiceImpl<SysElementRelEntity> implements ISysElementRelService  {


    @Autowired
    private SysElementRelDao dao;



    @Override
    public     List<SysElementRelJoin> initElementRel(String userId) {
        BaseModelDeleteWrapper.newInstance(SysElementRelEntity.class)
                .eq("sysUserId",userId)
                .execute();

        List<SysElementEntity> sysElementList =  BaseModelSelectWrapper.newInstance(SysElementEntity.class).selectList();
        List<SysElementRelJoin> relList =  sysElementList.stream().map(row -> {


            SysElementRelEntity rel =  new SysElementRelEntity();
            rel.setSysUserId(userId);
            rel.setSysElementId(row.getId());
            rel.setOpen(true);
            rel.createInsertWrapper().execute();

            SysElementRelJoin elementEntity = new SysElementRelJoin();
            elementEntity.setCode(row.getCode());
            elementEntity.setId(rel.getId());
            elementEntity.setElementType(row.getElementType().getValue());
            elementEntity.setSysUserId(userId);
            elementEntity.setTitle(row.getTitle());

            return elementEntity;
        }).collect(Collectors.toList());

        return relList;
    }
}
